home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 26
/
Cream of the Crop 26.iso
/
program
/
ddj0897.zip
/
AA897.ZIP
/
DRIVER.CPP
next >
Wrap
C/C++ Source or Header
|
1997-04-30
|
7KB
|
196 lines
// Driver program for cycle-less version of topological sorting
////////////////////////////////////////////////////////////////////
//
// Include files
#include "topsort.h"
// for now, the mmbers are strings
#include <string>
#include <iostream>
typedef pair< char **, char ** > Relation;
typedef vector< string, allocator < string > > stringVector;
// typedef pair< stringVector::iterator, stringVector::iterator > stringRelation;
typedef pair< string *, string * > stringRelation;
typedef vector< stringRelation, allocator < stringRelation > > stringRelations;
void exampleWithArrays()
{
char *names[8] = {"A", "B", "C", "D", "E", "F", "G", "H" };
char *results[8];
Relation rels[20];
int numSorted;
int numCycles;
int i;
int numRels = 0;
Cycle theCycles[20];
cout << "Example 1: B, A, C, D" << endl;
// b precedes a
rels[numRels++] = make_pair(&names[1], &names[0]);
// b precedes d
rels[numRels++] = make_pair(&names[1], &names[3]);
// a precedes c
rels[numRels++] = make_pair(&names[0], &names[2]);
// c precedes d
rels[numRels++] = make_pair(&names[2], &names[3]);
numSorted = topsort(names, names + 4, rels, rels + numRels, results);
// print out the results. The order should be B, A, C, D
if (numSorted != 4)
cout << "topsort reports there is a cycle " << endl;
else
cout << "topsort reports there is no cycle" << endl;
for (i = 0; i < numSorted; i++)
cout << results[i] << endl;
numCycles = cycles(names, names + 4, rels, rels + numRels, theCycles);
if (numCycles == 0)
cout << "cycles reports (correctly) that there is no cycle" << endl;
else
cout << "cycles reports (incorrectly) that there is a cycle" << endl;
cout << "Example 1A: B, A, [C D]" << endl;
// add in a recursive call: d precedes c. Now there is a cycle,
// between d and c, so the order printed by topsort should be B A
// c precedes d
rels[numRels++] = make_pair(&names[3], &names[2]);
numSorted = topsort(names, names + 4, rels, rels + numRels, results);
// print out the results. The order should be B, A
if (numSorted == 2)
cout << "topsort reports (correctly) that there is a cycle " << endl;
else if (numSorted == 4)
cout << "topsort reports (incorrectly) that there is no cycle " << endl;
else
cout << "topsort reports (incorrectly) that there is a cycle after " << numSorted << " elements" << endl;
for (i = 0; i < numSorted; i++)
cout << results[i] << endl;
numCycles = cycles(names, names + 4, rels, rels + numRels, theCycles);
if (numCycles == 0)
cout << "cycles reports (incorrectly) that there is no cycle" << endl;
else
cout << "cycles reports (correctly) that there is a cycle" << endl;
for (i = 0; i < numCycles; i++)
{
cout << "cycle " << i << ": ";
for (int j = 0; j < theCycles[i].size(); j++)
cout << names[theCycles[i][j]] << " ";
cout << endl;
}
bool haveCycle;
haveCycle = topsortWithCycles(names, names + 4, rels, rels + numRels, results);
// print out the results. The order should be B, A
if (haveCycle)
cout << "topsortWithCycles reports (correctly) that there is a cycle " << endl;
else
cout << "topsortWithCycles reports (incorrectly) that there is no cycle " << endl;
for (i = 0; i < 4; i++)
cout << results[i] << endl;
}
void exampleWithVectors()
{
stringVector strings;
int numSorted;
int numCycles;
int i;
cout << "Example 2: A, [B, C D], E, [F, G]" << endl;
strings.push_back("a");
strings.push_back("b");
strings.push_back("c");
strings.push_back("d");
strings.push_back("e");
strings.push_back("f");
strings.push_back("g");
stringRelations rels2;
// A precedes B
rels2.push_back(make_pair(&strings[0], &strings[1]));
// B precedes C
rels2.push_back(make_pair(&strings[1], &strings[2]));
// C precedes D
rels2.push_back(make_pair(&strings[2], &strings[3]));
// C precedes B
rels2.push_back(make_pair(&strings[2], &strings[1]));
// D precedes B
rels2.push_back(make_pair(&strings[3], &strings[1]));
// D precedes E
rels2.push_back(make_pair(&strings[3], &strings[4]));
// D precedes F
rels2.push_back(make_pair(&strings[3], &strings[5]));
// F precedes G
rels2.push_back(make_pair(&strings[5], &strings[6]));
// G precedes F
rels2.push_back(make_pair(&strings[6], &strings[5]));
stringVector stringResults(strings.size());
numSorted = topsort(strings.begin(),
strings.end(),
rels2.begin(),
rels2.begin() + rels2.size(),
stringResults.begin());
if (numSorted == 1)
cout << "topsort reports (correctly) that there is a cycle after 1 element" << endl;
else if (numSorted == strings.size())
cout << "topsort reports (incorrectly) that there is no cycle " << endl;
else
cout << "topsort reports (incorrectly) that there is a cycle after " << numSorted << " elements" << endl;
for (i = 0; i < numSorted; i++)
cout << stringResults[i] << endl;
vector < Cycle, allocator < Cycle > > cycleResults(strings.size());
numCycles = cycles(strings.begin(),
strings.end(),
rels2.begin(),
rels2.begin() + rels2.size(),
cycleResults.begin());
if (numCycles == 2)
cout << "cycles reports (correctly) that there are two cycles" << endl;
else if (numCycles == 0)
cout << "cycles reports (incorrectly) that there is no cycle" << endl;
else
cout << "cycles reports (incorrectly) that there are " << numCycles << " cycles" << endl;
for (i = 0; i < numCycles; i++)
{
cout << "cycle " << i << ": ";
for (int j = 0; j < cycleResults[i].size(); j++)
cout << strings[cycleResults[i][j]] << " ";
cout << endl;
}
bool haveCycle;
haveCycle = topsortWithCycles(strings.begin(),
strings.end(),
rels2.begin(),
rels2.begin() + rels2.size(),
stringResults.begin());
// print out the results. The order should be A [B C D] E [F G] or
// A [B C D] [F G] E
// where the bracketed members can occur in any order
if (haveCycle)
cout << "topsortWithCycles reports (correctly) that there is a cycle " << endl;
else
cout << "topsortWithCycles reports (incorrectly) that there is no cycle " << endl;
for (i = 0; i < strings.size(); i++)
cout << stringResults[i] << endl;
}
int main(int argc, char **argv)
{
exampleWithArrays();
exampleWithVectors();
return 0;
}